package com.xdb.sqlparser.ast.statement;

import com.xdb.sqlparser.ast.SQLExpr;
import com.xdb.sqlparser.visitor.SQLAstVisitor;
/**
 * 表示表抽象，内含别名和表
 * @author woate
 *
 */
public class SQLExprTableSource extends AbstractSQLTableSourceImpl {
	/**
	 * 表语句
	 */
	protected SQLExpr expr;

	public SQLExprTableSource() {

	}

	public SQLExprTableSource(SQLExpr expr) {
		this(expr, null);
	}

	public SQLExprTableSource(SQLExpr expr, String alias) {
		this.setExpr(expr);
		this.setAlias(alias);
	}
	/**
	 * 获取表语句
	 * @return
	 */
	public SQLExpr getExpr() {
		return this.expr;
	}
	/**
	 * 设置表语句
	 * @param expr
	 */
	public void setExpr(SQLExpr expr) {
		if (expr != null) {
			expr.setParent(this);
		}
		this.expr = expr;
	}

	@Override
	protected void accept0(SQLAstVisitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.expr);
		}
		visitor.endVisit(this);
	}

	public void output(StringBuffer buf) {
		this.expr.output(buf);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((expr == null) ? 0 : expr.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SQLExprTableSource other = (SQLExprTableSource) obj;
		if (expr == null) {
			if (other.expr != null)
				return false;
		} else if (!expr.equals(other.expr))
			return false;
		return true;
	}

}
